//
//  AppDelegate.m
//  EasyChat
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AppDelegate.h"
#import "ChatViewController.h"
#import "ContentViewController.h"
#import "DiscoveryViewController.h"
#import "UserViewController.h"

#import "LoginViewController.h"
#import "MyTabBarController.h"




@interface AppDelegate ()<EMChatManagerDelegate>

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
    
    //移除聊天管理器的代理
    [[EaseMob sharedInstance].chatManager removeDelegate:self];
    
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    // 所有导航栏的颜色设置
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:38 / 255.0 green:148 / 255.0 blue:234 / 255.0 alpha:0.9]];
     

    
    
    
    // 运行后进入登录页面
    LoginViewController *resginVC = [[LoginViewController alloc] init];
    resginVC.tabBar = self.tabBarController;
    
    
    
    
    //1.注册环信sdk
    [[EaseMob sharedInstance] registerSDKWithAppKey:@"mirco-ease#easychat" apnsCertName:@""];
    
    //写此句完成自动登录
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
    // 2.监听自动登录的状态
    // 设置chatManager代理
    // 写个nil 默认代理会在主线程调用
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    // 3.如果登录过，直接来到主界面
    if ([[EaseMob sharedInstance].chatManager isAutoLoginEnabled]) {
        
//        [se presentViewController:resginVC animated:YES completion:^{
//        
//        }];
        MyTabBarController *tabbar = [MyTabBarController new];
        self.window.rootViewController = tabbar;

    }else{
    self.window.rootViewController = resginVC;

    }
    
//    // 内存释放
//    [tabBar release];
//    [chatNavi release];
//    [contentNavi release];
//    [userNaci release];
//    [resginVC release];
     [_window release];
    
    return YES;
}

//协议代理方法
-(void)didAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error
{
    if (!error) {
        NSLog(@"自动登录成功 %@",loginInfo);
    }else{
        NSLog(@"自动登录失败 %@",error);
    }
}


#pragma mark - 好友添加的代理方法
//好友请求被同意了
-(void)didAcceptedByBuddy:(NSString *)username{
    
    NSString *message = [NSString stringWithFormat:@"%@ 同意了你的好友请求",username];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"好友添加消息" message:message delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alert show];
    
}

//好友请求被拒绝了

-(void)didRejectedByBuddy:(NSString *)username{
    
    NSString *message = [NSString stringWithFormat:@"%@ 拒绝了你的好友请求",username];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"好友添加消息" message:message delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alert show];
    
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

//app 进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application {
   
    [[EaseMob sharedInstance] applicationDidEnterBackground:application];

    
}

//app 将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    [[EaseMob sharedInstance] applicationWillEnterForeground:application];

}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

//申请处理时间
- (void)applicationWillTerminate:(UIApplication *)application {
    [[EaseMob sharedInstance] applicationWillTerminate:application];
    
}

@end
